<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['gettype']=array('1'=>'充值兑换','2'=>'活动赠送','3'=>'朋友赠送');
if($_RQ['op']=='list'){
	
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_goodscard',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['status']=array('0'=>'不显示','1'=>'显示');
	foreach($return['list'] as &$v){
		$v['createtime']=date('Y/m/d H:m:s',$v['createtime']);
		$getallnum = pdo_count('jy_weishop_goodscard_getlog',array('uniacid'=>$_SESSION['uniacid'],'cardid'=>$v['id']));
		if($v['t']<1){
			$v['last']='不限制';
		}else{
			$v['last']=$v['t']-$getallnum;
		}
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'购物卡列表'),
			array('op'=>'edit','title'=>'添加购物卡'),
		),
		'list'=>array('data'=>'list','edit_op'=>'edit','list'=>array(
			array('name'=>'title','title'=>'购物卡名称','type'=>'string'),
			array('name'=>'gettype','title'=>'获取方式','type'=>'radio','options'=>'gettype'),								
			array('name'=>'last','title'=>'剩余数量','type'=>'string'),
			array('name'=>'status','title'=>'购卡中心显示','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
		),
		),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'名称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_weishop_goodscard',array('id'=>$id));
	}else{
		$item=array();
	}
	$item['sendtitle']=$item['sendtitle']?$item['sendtitle']:'[nickname]:恭喜您，获得一个购物卡';
	$item['sendimage']=$item['sendimage']?$item['sendimage']:'../core/resource/images/h5game/card.png';
	$item['senddesc']=$item['senddesc']?$item['senddesc']:'购物卡的使用有时间限制，请及时使用！';
	$item['limittimes']=json_decode($item['limittimes'],true);
	$item['sendtime']=json_decode($item['sendtime'],true);
    $return['item']=$item;
	$goods=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'','id desc');
	foreach($goods as &$v){
		$id=$v['id'];
		$return['goods'][$id]=$v['title'];
	}
	$groups=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'','id desc');
	foreach($groups as &$v){
		$id=$v['id'];
		$return['goodgroup'][$id]=$v['title'];
	}
	$return['isdescsys']=array('0'=>'不使用','1'=>'使用');
	$return['status']=array('0'=>'不显示','1'=>'显示');
	$return['is']=array('0'=>'否','1'=>'是');
	$return['timelimittype']=array('0'=>'无限制','1'=>'获得多少天内','2'=>'时间段内');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'购物卡列表'),
			array('op'=>'edit','title'=>'添加购物卡'),
		),
		'desc'=>'购物卡设置',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本设置',
				'list'=>array(
					array('type'=>'string','name'=>'paixu','title'=>'排序','help'=>'越大越靠前'),
					array('type'=>'string','name'=>'title','title'=>'购物卡名称'),
					array('type'=>'image','name'=>'image','title'=>'图片'),
					array('type'=>'string','name'=>'money','title'=>'购物卡面额'),
					array('type'=>'string','name'=>'price','title'=>'购物卡价格','help'=>'花多少钱可购买本卡'),
					array('type'=>'radio','name'=>'timelimittype','title'=>'使用时间限制','options'=>'timelimittype'),
					array('type'=>'string','name'=>'timedays','title'=>'在领取后天数内有效','show'=>array('timelimittype'=>1)),
					array('type'=>'time_range','name'=>'limittimes','title'=>'在时间段内有效','show'=>array('timelimittype'=>2)),					
					array('type'=>'string','name'=>'t','title'=>'发放总数','help'=>'0为不限制'),
		)
	),
			array(
				'title'=>'领取设置',
				'list'=>array(
					array('type'=>'radio','name'=>'gettype','options'=>'gettype','title'=>'领取方式'),
					array('type'=>'radio','name'=>'status','options'=>'status','title'=>'加入购卡中心'),
					array('type'=>'switch','name'=>'issendtime','title'=>'限制发放时间'),
					array('type'=>'time_range','name'=>'sendtime','title'=>'发放时间','show'=>array('issendtime'=>1)),		
					array('type'=>'string','name'=>'getmax','title'=>'每人限领次数'),
				)
			),
			array(
				'title'=>'推送',
				'list'=>array(
					array('type'=>'string','name'=>'sendtitle','title'=>'推送标题','help'=>'变量 [nickname] 会员昵称 [total] 购物卡张数'),
					array('type'=>'image','name'=>'sendimage','title'=>'推送封面'),
					array('type'=>'string','name'=>'senddesc','title'=>'推送说明','help'=>'变量 [nickname] 会员昵称 [total] 购物卡张数'),	
				)
			),
			array(
				'title'=>'使用说明',
				'list'=>array(
					array('type'=>'radio','name'=>'isdescsys','options'=>'isdescsys','title'=>'是否使用统一说明','help'=>'统一说明在【基础设置】中设置，如果使用统一说明，则在购物卡说明前面显示统一说明'),
					array('type'=>'string','name'=>'desc','title'=>'使用说明','help'=>'','show'=>array('isdescsys'=>0)),					
				)
			),
	)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array_fields(array('money','issendtime','sendtime','paixu','title','getmax','price','gettype','image','timelimittype','timedays','limittimes','t','status','sendtitle','senddesc','sendimage','desc','isdescsys'),$postdata);
	$data['limittimes']=json_encode($data['limittimes']);
	$data['sendtime']=json_encode($data['sendtime']);
	$id=intval($postdata['id']);
	if($id){
				pdo_update('jy_weishop_goodscard',$data,array('id'=>$id));
				exi('设置成功！','','list');
	}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				$data['createtime']=TIMESTAMP;
				pdo_insert('jy_weishop_goodscard',$data);
				exi('添加成功！','','list');
	}
}

if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_goodscard',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_goodscard',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>